package com.np.toiectest;

import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.np.toiectest.dto.CauHoiDTO;
import com.np.toiectest.util.AudioUtil;
import com.np.toiectest.util.ConfigObject;

public class ListenningActivity extends Activity implements OnClickListener {

	public static final String IS_DONE = "Finish Photo Part";

	private TextView number;
	private ImageView photo;

	private RadioButton radionButtonA;
	private RadioButton radionButtonB;
	private RadioButton radionButtonC;
	private RadioButton radionButtonD;

	private Button btnNext;
	private Button btnCheck;
	private Button btnBack;

	private int currentNumber;
	private int startingNumber;
	private int totalQuestion;

	private ArrayList<CauHoiDTO> ds;

	private MediaPlayer mediaPlayer;
	public static String AUDIO_NAME;
	public static String PHOTO_FILE_PATH;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photo);

		number = (TextView) findViewById(R.id.textViewNumber);
		photo = (ImageView) findViewById(R.id.imageView);
		radionButtonA = (RadioButton) findViewById(R.id.radioReadingB);
		radionButtonB = (RadioButton) findViewById(R.id.radioButtonB);
		radionButtonC = (RadioButton) findViewById(R.id.radioButtonC);
		radionButtonD = (RadioButton) findViewById(R.id.radioButtonD);

		radionButtonA.setOnClickListener(this);
		radionButtonB.setOnClickListener(this);
		radionButtonC.setOnClickListener(this);
		radionButtonD.setOnClickListener(this);

		btnNext = (Button) findViewById(R.id.btnNext);
		btnCheck = (Button) findViewById(R.id.btnCheck);
		btnBack = (Button) findViewById(R.id.btnPrevious);

		btnNext.setOnClickListener(this);
		btnCheck.setOnClickListener(this);
		btnBack.setOnClickListener(this);

		getData();

	}

	@SuppressWarnings("unchecked")
	private void getData() {
		Bundle bundle = getIntent().getExtras();

		ds = (ArrayList<CauHoiDTO>) bundle
				.getSerializable(ConfigActivity.QUESTIONs);
		if (ds.size() < 1) {
			return;
			// Some messages must be added here
		}

		ConfigObject object = (ConfigObject) bundle
				.getSerializable(ConfigActivity.CONFIGURATION);

		startingNumber = object.getCauHoiHienTai();
		currentNumber = 0;
		totalQuestion = object.getSoLuongCauHoi();

		loadCauHoi();
	}

	private void loadCauHoi() {
		try {
			CauHoiDTO cauHoi = ds.get(currentNumber);
			radionButtonA.setText(cauHoi.getDapAnA());
			radionButtonB.setText(cauHoi.getDapAnB());
			radionButtonC.setText(cauHoi.getDapAnC());
			radionButtonD.setText(cauHoi.getDapAnD());

			// Photo
			PHOTO_FILE_PATH = "Photo/" + cauHoi.getLinkHinhAnh() + ".jpg";
			InputStream open = getAssets().open(PHOTO_FILE_PATH);
			Drawable d = Drawable.createFromStream(open, null);
			photo.setImageDrawable(d);

			// Audio

			AUDIO_NAME = cauHoi.getLinkAudio();
			AudioUtil.playAudio(this, mediaPlayer, AUDIO_NAME);
			AssetFileDescriptor descriptor = getAssets().openFd(
					"Audio/" + AUDIO_NAME + ".mp3");
			long start = descriptor.getStartOffset();
			long end = descriptor.getLength();
			MediaPlayer mediaPlayer = new MediaPlayer();
			mediaPlayer.setDataSource(descriptor.getFileDescriptor(), start,
					end);
			mediaPlayer.prepare();
			mediaPlayer.start();

			// set label process
			number.setText((currentNumber + startingNumber + 1) + " / "
					+ totalQuestion);

		} catch (Exception e) {
			e.toString();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnNext:
			if (currentNumber + 1 < ds.size()) {
				currentNumber++;
				stopMedia();
				loadCauHoi();
			} else {
				finish(ConfigActivity.REQUEST_CODE_ID_PHOTO_PART);
			}
			break;
		case R.id.btnPrevious:
			if (currentNumber - 1 >= 0) {
				currentNumber--;
				stopMedia();
				loadCauHoi();
			} else {
				// must be preplaced by the previous activity if existsed
			}
			break;
		case R.id.btnCheck:
			if (ds.get(currentNumber).getDapAnChon() == ds.get(currentNumber)
					.getDapAnDung()) {
				Toast.makeText(this, "It's correct!", Toast.LENGTH_SHORT)
						.show();
			} else {
				Toast.makeText(this, "It's incorrect!", Toast.LENGTH_LONG)
						.show();
			}
			break;
		case R.id.radioReadingB:
			ds.get(currentNumber).setDapAnChon(1);
			break;
		case R.id.radioButtonB:
			ds.get(currentNumber).setDapAnChon(2);
			break;
		case R.id.radioButtonC:
			ds.get(currentNumber).setDapAnChon(3);
			break;
		case R.id.radioButtonD:
			ds.get(currentNumber).setDapAnChon(4);
			break;
		default:
			break;
		}

	}

	private void stopMedia() {
		// if (mediaPlayer.isPlaying()) {
		// mediaPlayer.stop();
		// }

	}

	private void finish(int resultCode) {
		Intent intent = getIntent();

		int score = 200;
		intent.putExtra("score", score);
		intent.putExtra("currentNumber", currentNumber + startingNumber + 1);
		setResult(resultCode, intent);

		finish();
	}
}
